package com.example.market;

import android.os.AsyncTask;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class FragmentSearch extends Fragment {

EditText serch;
ListView serchlv;

List<ImageItem>items=new ArrayList<>();

ProgressBar progressBar2;

List<String>apps;
    private SearchAdapter adapter;

    @Override
    public void onViewCreated(View view,Bundle savedInstanceState) {
        super.onViewCreated(view,savedInstanceState);

        serch=view.findViewById(R.id.serch);
        serchlv=view.findViewById(R.id.serchlv);
        progressBar2=view.findViewById(R.id.progressBar2);

apps=Arrays.asList("天气","空调","应用市场","360全景");

        adapter = new SearchAdapter();

      serch.addTextChangedListener(new TextWatcher() {
          @Override
          public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

          }

          @Override
          public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

          }

          @Override
          public void afterTextChanged(Editable s) {
                  String text=s.toString();
                  List<String>name=SeacherApp(text);


                  if (String.valueOf(name).equals("[天气]")){

                     items.add(new ImageItem(R.drawable.weather,"天气"));
                     

                  }else if (String.valueOf(name).equals("[空调]")) {
                      items.add(new ImageItem(R.drawable.air,"空调"));
                      
              }
                serchlv.setAdapter(adapter);

                  if (text.equals("")){
                      items.clear();

                  }
          }
      });







    }

    private List<String> SeacherApp(String text) {
        List<String>name=new ArrayList<>();

        for (String app:apps){
            if (app.contains(text)){
                name.add(app);

            }
        }
        return name;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.activity_search, container, false);





    }


    private class SearchAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int positon) {
            return positon;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int positon, View view, ViewGroup viewGroup) {
            view=LayoutInflater.from(getContext()).inflate(R.layout.update,null);
            ImageView imgLogo=view.findViewById(R.id.imgLogo);
            TextView txt_name=view.findViewById(R.id.txt_name);
            Button btn_update=view.findViewById(R.id.btn_update);
            
            ImageItem item=items.get(positon);
            
            imgLogo.setImageResource(item.getImageView());
            txt_name.setText(item.getTextname());
            
            btn_update.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    progressBar2.setVisibility(View.VISIBLE);
                    btn_update.setText("下载");
                    Toast.makeText(getContext(), "正在下载", Toast.LENGTH_SHORT).show();
                    new Download().execute();
                }
            });
            return view;
        }
    }

    private class Download extends AsyncTask{
        /**
         * @param objects
         * @deprecated
         */
        @Override
        protected Object doInBackground(Object[] objects) {

            for (int i = 0; i < 100; i++) {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                publishProgress(i);
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Object[] values) {
            super.onProgressUpdate(values);
     
           progressBar2.setProgress((Integer) values[0]);
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            
            progressBar2.setVisibility(View.GONE);
            Toast.makeText(getContext(), "下载完成", Toast.LENGTH_SHORT).show();
        }
    }
}